/**
 * Created by qqlai on 2016/11/22.
 */
{
    angular.module('overviewsModule')
        .controller('OverviewsController', OverviewsController);
    OverviewsController.$inject = ['http.interfaces', '$state'];
    function OverviewsController(interfaces, $state) {
        let vm = this;
        let collect = {
            List: [],
            selected: {},
         
            active: Active,
            ch: false,
            h:0,
            display:true,
            getDormId: getDormId,
            Changed: Changed,
            getInterfacesData: getInterfacesData,
            college: [],
            data: {},
            departStat: {},
            dormitoryList: [],
            dormStat: [],
            logout:logout
        };
        collect.active();//进入控制器后调用一次
        vm.collect = collect;//输出集合，提供给模板调用


        /** Active方法
         * @desc 控制器触发时执行一次的方法
         * @constructor
         */
        function Active() {
                getInterfacesData();
                /*
                 轮播
                 */
                var mySwiper = new Swiper('.swiper-container', {
                    pagination: '.swiper-pagination'

                });
        }

        function getInterfacesData() {
            interfaces.schoolLeader().then(res=> {
                if (res.data.resultCode == 1) {
                    collect.data = res.data.data;
                    if(collect.data.departBedCount.length>1){
                        angular.element(document.querySelector("#sy")).height(collect.data.departBedCount.length*55+55);
                    var myChart = echarts.init(document.getElementById('sy'));

                    // 指定图表的配置项和数据
                    var option = {
                        barWidth: 24,
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                            }
                        },
                        legend: {
                            data: ['已住', '未住'],
                            x: 'right'
                        },
                        grid: {
                            left: '2%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true

                        },
                        xAxis: {
                            type: 'value'
                        },
                        yAxis: {
                            type: 'category',
                            data: []

                        },
                        series: [
                            {
                                name: '已住',
                                type: 'bar',
                                stack: '总量',
                                label: {
                                    normal: {
                                        show: false,
                                        position: 'insideRight'
                                    }
                                },
                                data: []
                            },
                            {
                                name: '未住',
                                type: 'bar',
                                stack: '总量',
                                label: {
                                    normal: {
                                        show: false,
                                        position: 'insideRight'
                                    }
                                },
                                data: []
                            }

                        ],
                        color: ['#cb3428', 'rgb(239,169,94)']
                    };
                   
                    for (var x = 0; x < collect.data.departBedCount.length; x++) {
                        if (collect.data.departBedCount[x].departName.length > 6) {
                            option.yAxis.data[x] = collect.data.departBedCount[x].departName.replace(/(.{6})/g, '$1\n');

                        } else {
                            option.yAxis.data[x] = collect.data.departBedCount[x].departName;
                        }
                     
                        option.series[0].data[x] = collect.data.departBedCount[x].inCount ;
                        option.series[1].data[x] = collect.data.departBedCount[x].freeCount;


                    }



                       
                    // 使用刚指定的配置项和数据显示图表。
                    myChart.setOption(option);
                    }
                    else{
                        collect.display=false;
                    }
                }
            });
            interfaces.departmentList().then(res=> {
                if (res.data.resultCode == 1) {
                    collect.dormitoryList = res.data.data;

                    collect.List = res.data.data;
                        collect.List.unshift({
                            "departName": "全部",
                            "departCode": 0,
                            "departId": 1
                        },);

                    if (collect.List.length > 0) {
                        collect.selected = collect.List[0];
                    }
                }
            });
            interfaces.dormitoryList().then(res=> {
                if (res.data.resultCode == 1) {
                    collect.dormitoryList = res.data.data;
                }
            });
            interfaces.dormStat().then(res=> {
                if (res.data.resultCode == 1) {
                    collect.dormStat = res.data.data;
                }
            });
        }

        function getDormId(id, dormName) {
            $state.go('tab.dormitoryBuilding1', {id: id});
            interfaces.dormName = dormName;
        }

        function getdepartStat() {
            interfaces.departStat({departId: collect.selected.departId}).then(res=> {
                console.log(res.data.result);
                if (res.data.result == 1) {
                    collect.departStat = res.data.data;


                    var firstChart = echarts.init(document.getElementById('fir'));
                    // 指定图表的配置项和数据
                    var option = {
                        tooltip: {
                            trigger: 'item',
                            formatter: "{a} <br/>{b}: {c} ({d}%)"
                        },
                        grid: {
                            top: '5%'
                        },
                        legend: {
                            // orient: 'vertical',
                            x: 'left',
                            data: []
                        },
                        title: {
                            text: '各年级人数比例',
                            x: 'center',
                            y: 'center',
                            textStyle: {
                                fontSize: 14,
                                color: '#aaa'

                            },
                            // height:30,
                            lineHeight: 30
                        },
                        // Axis: {
                        //     type: 'category',
                        //     data:['空床位','已住']
                        // },
                        series: [

                            {
                                name: '院总览',
                                type: 'pie',
                                radius: ['40%', '55%'],
                                avoidLabelOverlap: false,

                                data: []
                            }
                        ],
                        color: ['rgb(189,86,94)', 'rgb(225,114,46)', 'rgb(239,169,102)', 'rgb(203,203,203)', 'rgb(225,80,80)', 'rgb(255,209,58)', 'rgb(219,163,145)', 'rgb(161,161,161)']

                    };

                    // 使用刚指定的配置项和数据显示图表。

                    for (var x in collect.departStat.gradeLodgerCount) {
                        var tem = [];
                        option.legend.data.push(x + '级');
                        tem.push(collect.departStat.gradeLodgerCount[x]);
                        option.series[0].data.push({value: collect.departStat.gradeLodgerCount[x], name: x + '级'});
                    }

                    firstChart.setOption(option);
                    var secondChart = echarts.init(document.getElementById('sen'));
                    // 指定图表的配置项和数据
                    var option = {
                        tooltip: {
                            trigger: 'item',
                            formatter: "{a} <br/>{b}: {c} ({d}%)"
                        },
                        legend: {
                            // orient: 'vertical',
                            x: 'left',
                            data: []
                        },
                        title: {
                            text: '男女人数比例',
                            x: 'center',
                            y: 'center',
                            textStyle: {
                                fontSize: 14,
                                color: '#aaa'

                            },
                            // height:30,
                            lineHeight: 30
                        },
                        // Axis: {
                        //     type: 'category',
                        //     data:['空床位','已住']
                        // },
                        series: [

                            {
                                name: '院总览',
                                type: 'pie',
                                radius: ['40%', '55%'],
                                avoidLabelOverlap: false,

                                data: []
                            }
                        ],
                        color: ['rgb(239,169,94)', 'rgb(191,85,93)']

                    };

                    for (var b in collect.departStat.sexLodgerCount) {
                        var tem2 = [];
                        option.legend.data.push(b);
                        tem2.push(collect.departStat.sexLodgerCount[b]);
                        option.series[0].data.push({value: collect.departStat.sexLodgerCount[b], name: b});
                    }
                    secondChart.setOption(option);
                }
            });
        }

        function Changed() {
            if (collect.selected.departId == 1) {
                collect.ch = false;
                interfaces.dormStat().then(res=> {
                    if (res.data.resultCode == 1) {
                        collect.dormStat = res.data.data;
                    }
                });
            } else {
                collect.ch = true;
                getdepartStat();
                interfaces.dormStat({departId:collect.selected.departId}).then(res=> {
                    if (res.data.resultCode == 1) {
                        collect.dormStat = res.data.data;
                    }
                });
            }
        }

        function logout() {
            interfaces.logout();
        }
    }
}